﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Remit
{
    public class PaymentAppService : ApplicationService, IPaymentAppService
    {
        private readonly IRepository<Founder, int> _founderRepository;
        private readonly IRepository<Payment, int> _paymentRepository;
        private readonly LogisticsDbContext _logisticsDbContext;

        public PaymentAppService(IRepository<Founder, int> founderRepository, IRepository<Payment, int> paymentRepository, LogisticsDbContext logisticsDbContext)
        {
            _logisticsDbContext = logisticsDbContext;
            _founderRepository = founderRepository;
            _paymentRepository = paymentRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Procurement"></param>
        /// <returns></returns>
        public async Task<PaymentDto> CreateAsync(PaymentDto Payment)
        {
            var payment = new Payment
            {
                Applicant=Payment.Applicant,
                Bank=Payment.Bank,
                BankAccount=Payment.BankAccount,
                PaymentObject=Payment.PaymentObject,
                PaymentPrice=Payment.PaymentPrice,
                PaymentType=Payment.PaymentType,
                PurposePescription=Payment.PurposePescription,
                Remarks=Payment.Remarks,
                title= Payment.title,
                CreateTime=DateTime.Now,
                PaymentTime=DateTime.Now,
            };

            await _paymentRepository.InsertAsync(payment);
            return ObjectMapper.Map<Payment, PaymentDto>(payment);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [RemoteService(false)]
        public async Task DeleteAsync(int id)
        {
            await _paymentRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PaymentDto> GetAsync(int id)
        {
            var payment = await _paymentRepository.GetAsync(id);
            return ObjectMapper.Map<Payment, PaymentDto>(payment);
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        public async Task<PagedResultDto<PaymentDto>> GetListAsync(PaymentListDto dto)
        {
            var Pay = _logisticsDbContext.Payment.ToList();
            var founder = _logisticsDbContext.Founder.ToList();
            var joinpay = from a in Pay
                           join c in founder on a.Applicant equals c.Id
                           select new Payment
                           {
                               Id=a.Id,
                               Applicant=a.Applicant,
                               BankAccount=a.BankAccount,
                               Bank=a.Bank,
                               CreateTime=DateTime.Now,
                               PaymentObject=a.PaymentObject,
                               PaymentPrice=a.PaymentPrice,
                               PaymentTime=DateTime.Now,
                               PaymentType=a.PaymentType,
                               PurposePescription=a.PurposePescription,
                               Remarks=a.Remarks,
                               title=a.title,
                               Name=c.Name,
                           };
            int totoCount = joinpay.Count();
            var data = new PagedResultDto<PaymentDto>();
            data.TotalCount = totoCount;
            joinpay = joinpay.Skip((dto.page - 1) * dto.limit).Take(dto.limit).ToList();
            data.Items = ObjectMapper.Map<List<Payment>, List<PaymentDto>>(joinpay.ToList());
            return data;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Gathering"></param>
        /// <returns></returns>

        public int UpdateAsync(PaymentDto paymentdto)
        {
            var payment = new Payment
            {
                Applicant=paymentdto.Applicant,
                BankAccount=paymentdto.BankAccount,
                Bank=paymentdto.Bank,
                CreateTime=DateTime.Now,
                Id=paymentdto.Id,
                PaymentObject= paymentdto.PaymentObject,
                PaymentPrice=paymentdto.PaymentPrice,
                PaymentTime=paymentdto.PaymentTime,
                PaymentType=paymentdto.PaymentType,
                PurposePescription=paymentdto.PurposePescription,
                Remarks=paymentdto.Remarks,
                title= paymentdto.title
            };
            _logisticsDbContext.Entry(payment).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            var i = _logisticsDbContext.SaveChanges();
            return i;
        }
    }
}
